<!--
 * @Autor: dingxiaolin
 * @Date: 2021-01-08 13:59:57
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-01-28 18:30:56
-->
<template>
    <!--上传缩略图模态框-->
    <el-dialog
        title="选择文件"
        :visible.sync="showdialog"
        width="36%"
        :close-on-click-modal="false"
        :show-close="false"
    >
        <div class="tp-box">
            <div class="container">
                <div class="imageBox img-container">
                    <img id="tailoringImg" alt />
                    <div class="spinner" style="display: none">Loading...</div>
                </div>
                <div class="tp-point">推荐640x640像素，jpg/jpeg/png，5M以内图片</div>
                <div class="cropped2 docs-preview">
                    <div class="square previewImg"></div>
                </div>
                <div class="cropped-txt2">图片预览</div>
                <div class="new-contentarea tc">
                    <label title="上传头像" for="chooseImg" class="l-btn choose-btn upload-img">
                        <input
                            type="file"
                            accept="image/jpg, image/jpeg, image/png"
                            name="file"
                            id="chooseImg"
                            class="hidden"
                            @change="selectImg($event, 'realHead')"
                        />
                        上传图片
                    </label>
                </div>
            </div>
            <div class="tp-btn-group">
                <el-button type="default" size="medium" @click="$emit('update:showdialog', false)"
                    >取 消</el-button
                >
                <el-button type="primary" size="medium" @click="confirm()">保 存</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
import { uploadStream } from '@/common/util/ossUploadService';
import Cropper from 'cropperjs';
@Component({})
export default class CropperDialogue extends Vue {
    @Prop({ default: false })
    showdialog;
    @Prop({ default: '' })
    url;
    @Prop({ default: '' })
    name;
    /**
     * logo选择插件
     */
    public cropper: any;
    /**头像图片名称 */
    public avatarName = '';
    @Watch('showdialog')
    change(v) {
        if (v) {
            this.$nextTick(() => {
                /* 头像选择 */
                let img = document.getElementById('tailoringImg') as HTMLCanvasElement;
                this.cropper = new Cropper(img, {
                    aspectRatio: 1 / 1,
                    dragMode: 'move',
                    preview: '.previewImg', // 预览视图
                    guides: false, // 裁剪框的虚线(九宫格)
                    autoCropArea: 0.7, // 0-1之间的数值，定义自动剪裁区域的大小，默认0.8
                    movable: true, // 是否允许移动剪裁框
                    zoomable: true, // 是否允许缩放图片大小
                    rotatable: true, // 是否允许旋转图片
                    cropBoxMovable: false, //是否允许拖动裁剪框
                    cropBoxResizable: false, //是否允许拖动 改变裁剪框大小
                    ready: function (e) {
                        $('.cropper-face').html(
                            '<div class="face-guide" data-cropper-action="all"></div>'
                        );
                    },
                    crop: function (e) {
                        // 输出结果数据裁剪图像。
                    },
                });
                if (this.$store.state.blobUrl) {
                    this.cropper.replace(this.$store.state.blobUrl);
                }
            });
        }
    }
    close() {
        this.$emit('update:showdialog', false);
    }
    mounted() {}
    getObjectURL(file) {
        var url = null;
        if ((window as any).createObjectURL != undefined) {
            // basic
            url = (window as any).createObjectURL(file);
        } else if (window.URL != undefined) {
            // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) {
            // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
    /* 头像上传 */
    public selectImg(event) {
        let file = event.target.files[0];
        if (file) {
            var fileMaxSize = 1024 * 5; //5M
            var fileSize = file.size;
            var size = fileSize / 1024;
            //   console.log(size);
            // this.imgSize = this.readFileSize(file.size);
            this.avatarName = file.name;
            if (size > fileMaxSize) {
                this.$message.warning(`上传图片大小不能超过 5MB`);
            } else {
                this.$store.state.blobUrl = this.getObjectURL(file);
                if (this.cropper) {
                    this.cropper.replace(this.$store.state.blobUrl);
                }
            }
        } else {
            this.$message.warning(`请选择图片！`);
            return null;
        }
    }
    /* 确认保存头像 */
    public confirm() {
        if (!this.avatarName) {
            this.$message.warning(`请先上传图片`);
            return;
        }
        uploadStream(this.cropper.getCroppedCanvas().toDataURL('image/png'), (res) => {
            if (res.res.statusCode == '200') {
                this.$emit('update:showdialog', false);
                this.$emit('update:url', res.url);
                this.$emit('update:name', this.avatarName);
            } else {
                this.$message.error('图片上传失败!');
            }
        });
    }
}
</script>

<style lang="scss" scoped>
/**上传头像模态框 begin**/
.tp-box {
    width: 100%;
    background: #ffffff;
}
.tp-title {
    font-family: PingFangSC-Medium;
    color: #333333;
    letter-spacing: 0;
    text-align: center;
    line-height: 60px;
    margin-bottom: 2%;
}
.tp-point {
    font-family: PingFangSC-Regular;
    color: #606266;
    letter-spacing: 0;
    line-height: 60px;
    font-size: 16px;
}
.container {
    width: 400px;
    position: relative;
    font-family: 微软雅黑;
    margin: 0;
    font-size: 18px;
}
.container p {
    line-height: 12px;
    line-height: 0px;
    height: 0px;
    margin: 10px;
    color: #bbb;
}
.action {
    width: 400px;
    height: 30px;
    margin: 10px 0;
}
.cropped2 {
    position: absolute;
    right: -240px;
    top: 0;
    width: 200px;
    border: 1px solid #dcdfe6;
    background: #f6f6f6;
    height: 200px;
    padding: 4px;
    box-shadow: 0px 0px 12px #ddd;
    text-align: center;
    box-sizing: content-box;
}
.cropped-txt2 {
    position: absolute;
    right: -240px;
    top: 200px;
    width: 200px;
    height: 60px;
    font-family: PingFangSC-Regular;
    color: #606266;
    letter-spacing: 0;
    text-align: center;
    line-height: 60px;
}
.imageBox {
    position: relative;
    height: 400px;
    width: 400px;
    background: #fff;
    overflow: hidden;
    background-repeat: no-repeat;
    cursor: move;
    background: url('');
}
.imageBox .thumbBox {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 200px;
    height: 200px;
    margin-top: -100px;
    margin-left: -100px;
    box-sizing: border-box;
    border: 1px solid rgb(102, 102, 102);
    box-shadow: 0 0 0 1000px rgba(0, 0, 0, 0.5);
    background: none repeat scroll 0% 0% transparent;
}
.imageBox .spinner {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    text-align: center;
    line-height: 400px;
    background: rgba(0, 0, 0, 0.7);
}
.Btnsty_peyton {
    width: 66px;
    display: block;
    float: right;
    margin-bottom: 10px;
    height: 57px;
    line-height: 57px;
    color: #ffffff;
    margin: 0px 2px;
    background-color: #f38e81;
    border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    -ms-border-radius: 3px;
    -o-border-radius: 3px;
    text-decoration: none;
    cursor: pointer;
    box-shadow: 0px 0px 5px #b0b0b0;
    border: 0px #fff solid;
}
.new-contentarea {
    width: 100px;
    height: 30px;
    line-height: 30px;
    position: absolute;
    right: -140px;
    top: 360px;
    cursor: pointer;
    border: 1px solid #999;
}
.new-contentarea label {
    width: 100%;
    height: 100%;
    display: block;
    cursor: pointer;
    font-size: 14px;
    text-align: center;
}
.new-contentarea input[type='file'] {
    width: 100px;
    height: 40px;
    position: absolute;
    right: 50%;
    margin-right: -50px;
    top: 0;
    visibility: hidden;
    z-index: 2;
    cursor: pointer;
}
.previewImg {
    width: 200px;
    height: 200px;
    overflow: hidden;
    border-radius: 50%;
    border: 1px dashed #666;
}

/**上传头像模态框 End**/
</style>
